package com.czmh.etc.interpreter.entity.base;

import java.util.Currency;

/**
 * Created by Huangyk on 2019/6/4.
 */
public class Header extends HeaderBase {
    public static int HEADER_LEN = 48;

    public Header(EnMessagetype msgType){
        SetHeader(msgType, 0);
    }

    public Header(String header){
        int currPos = 0;

        this.MsgLen = new Integer(header.substring(currPos, 8));
        currPos += 8;

        this.MsgID = new Integer(header.substring(currPos, 2));
        currPos += 2;

        //
        int msgType = new Integer(header.substring(currPos, 4));
        currPos += 4;
        this.MsgType = EnMessagetype.loadEnMessageTypeEnum(msgType);

        this.SenderOrgCode = header.substring(currPos, 7);
        currPos += 7;

        this.RecvOrgCode = header.substring(currPos, 7);
        currPos += 7;

        this.SendTime = header.substring(currPos, 14);
        currPos += 14;

        this.VerifyCode = Long.parseLong(header.substring(currPos, 8), 16);
    }

    public Integer MsgLen;

    /**
     * @Description
     * 报文编码
     * 91：银行前置机 -> 客服中心前置机
     * 92：客服中心前置机 -> 银行前置机
     */
    private Integer MsgID;

    /**
     * 报文类型，由EnMessagetype定义
     */
    private EnMessagetype MsgType;

    /**
     * 发送方机构代码
     */
    private String SenderOrgCode;

    /**
     * 接收方机构代码
     */
    private String RecvOrgCode;

    /**
     * 发送时间
     * 格式：yyyyMMddHHmmss
     */
    private String SendTime;

    /**
     * CRC32校验码
     * 报文体的CRC32校验
     */
    private Long VerifyCode;

    public void SetHeader(EnMessagetype msgType, Integer msgLen){
        this.MsgID = new Integer(msgType.getMessageType() / 100);
        this.MsgType = msgType;
        this.MsgLen = msgLen;
        if(msgLen % 8 > 0 && this.MsgType != EnMessagetype.KeyRequest){
            this.MsgLen += 8 - this.MsgLen % 8;
        }
    }

    public static String FillCrcData(String req, Integer crc){
        String c = String.format("%08x", crc);
        String ret = req.substring(0, Header.HEADER_LEN - 8) + c + req.substring(Header.HEADER_LEN, req.length() - Header.HEADER_LEN);

        return ret;
    }

    @Override
    public String toString(){
        return (String.format("%08d", this.MsgLen) +
                String.format("%04d", this.MsgType) +
                this.SenderOrgCode +
                this.RecvOrgCode +
                this.SendTime +
                String.format("%08x", this.VerifyCode)
                );
    }

    //region getter && setter
    public Integer getMsgLen() {
        return MsgLen;
    }

    public void setMsgLen(Integer msgLen) {
        MsgLen = msgLen;
    }

    public Integer getMsgID() {
        return MsgID;
    }

    public void setMsgID(Integer msgID) {
        MsgID = msgID;
    }

    public EnMessagetype getMsgType() {
        return MsgType;
    }

    public void setMsgType(EnMessagetype msgType) {
        MsgType = msgType;
    }

    public String getSenderOrgCode() {
        return SenderOrgCode;
    }

    public void setSenderOrgCode(String senderOrgCode) {
        SenderOrgCode = senderOrgCode;
    }

    public String getRecvOrgCode() {
        return RecvOrgCode;
    }

    public void setRecvOrgCode(String recvOrgCode) {
        RecvOrgCode = recvOrgCode;
    }

    public String getSendTime() {
        return SendTime;
    }

    public void setSendTime(String sendTime) {
        SendTime = sendTime;
    }

    public Long getVerifyCode() {
        return VerifyCode;
    }

    public void setVerifyCode(Long verifyCode) {
        VerifyCode = verifyCode;
    }
    //endregion
}
